<?php

namespace App\Http\Controllers;

use App\Http\Requests\IndexPublicRequest;
use App\Http\Resources\ActivityResource;
use App\Http\Resources\BlindActivityResource;
use App\Http\Resources\BlindCateResource;
use App\Http\Resources\BlindResource;
use App\Models\Activity;
use App\Models\Blind;
use App\Models\BlindActivity;
use App\Models\BlindCate;
use App\Models\ShopCate;
use App\Models\Swiper;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class PublicController extends Controller
{

    public function swiper(Request $request, Swiper $swiper)
    {
        $data = $swiper->where('status', '1')->orderBy('order')->get();
        return $this->success('查询成功', $data);
    }


    public function activity(IndexPublicRequest $request, Activity $activity)
    {

        $data = $activity->where('status', '1')->paginate(
            $request->query('pageSize'),
            ['*'],
            'page',
            $request->query('pageNum')
        );
        $response = ActivityResource::collection($data);
        $total = $response->total(); // 获取分页信息中的总数
        return $this->success('查询成功', $response, $total);
    }
    public function blind(IndexPublicRequest $request)
    {

        $query = Blind::query();
        $query->when($request->filled('name'), function ($query) use ($request) {
            $name = $request->input('name');
            return $query->where(function ($subquery) use ($name) {
                $subquery->where('name', 'like', "%$name%");
            });
        })->when($request->filled('blind_cate_id'), function ($query) use ($request) {
            return $query->where('blind_cate_id', $request->input('blind_cate_id'));
        })->when($request->filled('blind_type'), function ($query) use ($request) {
            return $query->where('blind_type', $request->input('blind_type'));
        })->when($request->filled('type'), function ($query) use ($request) {
            return $query->where('type', $request->input('type'));
        })->when($request->filled('status'), function ($query) use ($request) {
            return $query->where('status', $request->input('status'));
        });

        $data = $query->with(['admins' => function ($query) {
            return $query->select('id', 'username');
        }, 'blindcates' => function ($query) {
            return $query->select('id', 'name');
        }, 'blindshops', 'statuslabel', 'blindtypelabel', 'typelabel'])->orderBy('created_at', 'DESC')->paginate(
            $request->query('pageSize'),
            ['*'],
            'page',
            $request->query('pageNum')
        );
        $response =  BlindResource::collection($data);
        $total = $response->total();
        return $this->success('查询成功', $response, $total);
    }

    /**
     * Display a listing of the resource.
     */
    public function blindcate(IndexPublicRequest $request)
    {
        $blindCate = BlindCate::query();

        $data = $blindCate->where('status', '1')->with('statuslabel')->orderBy('order', 'ASC')->paginate(
            $request->query('pageSize'),
            ['*'],
            'page',
            $request->query('pageNum')
        );
        $response =  BlindCateResource::collection($data);
        $total = $response->total();
        return $this->success('查询成功', $response, $total);
    }

    public function blindactivity(IndexPublicRequest $request, BlindActivity $blindActivity, $id)
    {
        Validator::make([
            'activitie_id' => $request->input('activitie_id')
        ], [
            'activitie_id' => ['required']
        ])->validate();

        $data = $blindActivity->where('status', '1')->where('activitie_id', $id)
            ->with(['blinds.blindtypelabel', 'activitys', 'blinds.blindcates'])->orderBy('order')->paginate(
                $request->query('pageSize'),
                ['*'],
                'page',
                $request->query('pageNum')
            );
        $response =  BlindActivityResource::collection($data);
        $total = $response->total();
        return $this->success('查询成功', $response, $total);
    }
}
